﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using BadmintonApp.Entity;
using BadmintonApp.DAO.Interface;

namespace BadmintonApp.DAO.Implement
{
    public class ActivityDAO : BaseDAO, IActivityDAO
    {
        /// <summary>
        /// 获取最大活动ID
        /// </summary>
        /// <returns>最大活动ID</returns>
        private int GetMaxActivityID()
        {
            string sql = "Select IsNull(Max(FActivityID), 0) from TActivitys";
            DbCommand selectCmd = db.GetSqlStringCommand(sql);
            return (int)db.ExecuteScalar(selectCmd);
        }

        #region IActivityDAO Members

        /// <summary>
        /// 插入一个活动
        /// </summary>
        /// <param name="activityInfo">活动信息类</param>
        public void InsertActivity(TActivityInfo activityInfo)
        {
            StringBuilder sbSQL = new StringBuilder();
            sbSQL.Append("Insert into TActivitys(FActivityID, FTitle, FLocation, FDate, FMoney, FContent) Values ( ");
            sbSQL.Append(" @FActivityID, @FTitle, @FLocation, @FDate, @FMoney, @FContent)");
            DbCommand insertCmd = db.GetSqlStringCommand(sbSQL.ToString());
            activityInfo.FActivityID = GetMaxActivityID() + 1;
            db.AddInParameter(insertCmd, "FActivityID", DbType.Int32, activityInfo.FActivityID);
            db.AddInParameter(insertCmd, "FTitle", DbType.String, activityInfo.FTitle);
            db.AddInParameter(insertCmd, "FLocation", DbType.String, activityInfo.FLocation);
            db.AddInParameter(insertCmd, "FDate", DbType.Date, activityInfo.FDate);
            db.AddInParameter(insertCmd, "FMoney", DbType.Decimal, activityInfo.FMoney);
            db.AddInParameter(insertCmd, "FContent", DbType.String, activityInfo.FContent);
            db.ExecuteNonQuery(insertCmd);
        }

        /// <summary>
        /// 更新一个活动那个
        /// </summary>
        /// <param name="activityInfo">活动信息类</param>
        public void UpdateActivity(TActivityInfo activityInfo)
        {
            StringBuilder sbSQL = new StringBuilder();
            sbSQL.Append("Update TActivitys Set FTitle=@FTitle, FLocation=@FLocation, ");
            sbSQL.Append(" FDate=@FDate, FMoney=@FMoney, FContent=@FContent ");
            sbSQL.Append(" Where FActivityID=@FActivityID");

            DbCommand updateCmd = db.GetSqlStringCommand(sbSQL.ToString());
            db.AddInParameter(updateCmd, "FActivityID", DbType.Int32, activityInfo.FActivityID);
            db.AddInParameter(updateCmd, "FTitle", DbType.String, activityInfo.FTitle);
            db.AddInParameter(updateCmd, "FLocation", DbType.String, activityInfo.FLocation);
            db.AddInParameter(updateCmd, "FDate", DbType.Date, activityInfo.FDate);
            db.AddInParameter(updateCmd, "FMoney", DbType.Decimal, activityInfo.FMoney);
            db.AddInParameter(updateCmd, "FContent", DbType.String, activityInfo.FContent);
            db.ExecuteNonQuery(updateCmd);
        }

        /// <summary>
        /// 删除一个活动
        /// </summary>
        /// <param name="activityID">活动ID</param>
        public void DeleteActivity(int activityID)
        {
            string sql = "Delete from TActivitys Where FActivityID=@FActivityID";
            DbCommand deleteCmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(deleteCmd, "FActivityID", DbType.Int32, activityID);
            db.ExecuteNonQuery(deleteCmd);
        }

        /// <summary>
        /// 获取所有活动
        /// </summary>
        /// <returns>活动信息类列表</returns>
        public DataSet GetAllActivity()
        {
            string sql = "Select * from TActivitys";
            DbCommand selectCmd = db.GetSqlStringCommand(sql);
            DataSet dsActivity = db.ExecuteDataSet(selectCmd);
            return dsActivity;
        }

        #endregion
    }
}
